package com.company.bookstore.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.company.bookstore.entities.book_module.BookImg;
import com.company.bookstore.mapper.book_module.BookImgMapper;
import com.company.bookstore.service.book_module.BookImgService;
import org.apache.ibatis.scripting.xmltags.ForEachSqlNode;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 姬深威
 * @since 2020-09-12
 */
@Service
@Transactional(readOnly = false)
public class BookImgServiceImpl extends ServiceImpl<BookImgMapper, BookImg> implements BookImgService {

    @Resource
    BookImgMapper bookImgMapper;

    @Override
    public void addImgs(int bookId, List<BookImg> imgs) throws Exception {
        for (int i = 0; i < imgs.size(); i++) {
            Integer affectRow = bookImgMapper.addImg(imgs.get(i).getImgName(), bookId);
            if (affectRow == null || affectRow.equals(0)){
                throw new Exception("添加失败");
            }
        }
    }

    @Override
    public Integer deleteImg(Integer bookId, Integer imgId) {
        int affectRow = bookImgMapper.deleteImgByImgId(bookId,imgId);
        return affectRow;
    }

    @Override
    public void deleteImgList(Integer bookId, Integer[] imgIds) throws Exception {
        for (Integer imgId : imgIds) {
            Integer affectRow = bookImgMapper.deleteImgByImgId(bookId, imgId);
            if (affectRow == null || affectRow.equals(0)){
                throw new Exception("删除失败");
            }
        }
    }
}
